package com.dmap.admin.controller.background;

import com.dmap.admin.po.AdminUser;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.service.backgroundv1.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 11:28
 * \* Description:
 * \
 */
@Api(value = "登录", description = "background/登录")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v1/user/login")
public class LoginController {

    @Resource
    private UserService userService;

    @ResponseBody
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public BaseVo login(@RequestParam String account, @RequestParam String password) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }

        AdminUser adminUser = userService.selectByAccount(account, password);
        //数据库查询是不区分大小写的，所以要加后面两个判断
        if (adminUser == null || !adminUser.getAccount().equals(account) || !adminUser.getPassword().equals(password)) {
            baseVo.setCode(AdminConstant.RESPONSE_LOGIN_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_LOGIN_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }
}
